package com.example.highclasscomponent.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.highclasscomponent.pojo.BillInfo;

import java.util.ArrayList;
import java.util.List;

public class BillDBHelper extends SQLiteOpenHelper {
    static String DB_NAME = "bill.db";
    static int DB_VERSION = 1;
    private static BillDBHelper mHelper = null;

    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;
    private static String TABLE_NAME = "bill";

    private BillDBHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static BillDBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new BillDBHelper(context);
        }
        return mHelper;
    }

    //打开读连接
    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    //打开写连接
    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = mHelper.getReadableDatabase();
        }
        return mWDB;
    }

    //关闭数据库连接
    public void closeLink() {
        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }
        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }


    @Override
    public void onCreate(SQLiteDatabase db) {


        String create_sql = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"(_id INTEGER PRIMARY KEY  AUTOINCREMENT NOT NULL," +
                "date VARCHAR NOT NULL,type VARCHAR NOT NULL,amount DOUBLE NOT NULL,remark VARCHAR NOT NULL);";
        db.execSQL(create_sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public long insert(BillInfo info) {
        ContentValues values = new ContentValues();
        values.put("date", info.date);
        values.put("type", info.type);
        values.put("amount", info.amount);
        values.put("remark", info.remark);
        return mWDB.insert(TABLE_NAME,null,values);

    }
    @SuppressLint("Range")
    public List<BillInfo> queryByMonth(String yearMonth)
    {
        String sql="select * from "+TABLE_NAME+" where date like '"+yearMonth+"%'";
        Cursor cursor = mRDB.rawQuery(sql, null);
        List<BillInfo>list=new ArrayList<>();
        while(cursor.moveToNext())
        {
            BillInfo info=new BillInfo();
            info.id=cursor.getInt(cursor.getColumnIndex("_id"));
            info.date=cursor.getString(cursor.getColumnIndex("date"));
            info.type=cursor.getInt(cursor.getColumnIndex("type"));
            info.amount=cursor.getDouble(cursor.getColumnIndex("amount"));
            info.remark=cursor.getString(cursor.getColumnIndex("remark"));
            list.add(info);
        }
        return list;
    }


}
